<template>
  <div class="app-container">
    <el-card>
      <!-- 搜索 -->
      <el-form :inline="true" :model="listQuery" size="small">
        <el-form-item>
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="listQuery.name"
            @clear="getCategory"
          >
            <el-button slot="append" icon="el-icon-search" @click="getCategory">
            </el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click="addCategory"
          >
            新增工程类型
          </el-button>
        </el-form-item>
      </el-form>
      <!-- 列表 -->
      <el-table
        style="width: 100%"
        v-loading="listLoading"
        highlight-current-row
        border
        size="small"
        :data="categoryList"
      >
        <el-table-column label="#" align="center" type="index" width="50">
        </el-table-column>
        <el-table-column prop="id" align="center" label="类型ID">
        </el-table-column>
        <el-table-column prop="name" align="center" label="类型名称">
        </el-table-column>
        <el-table-column align="center" label="操作" width="200px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 弹框 -->
    <el-dialog
      :title="edit ? '修改工程名称' : '新增工程类型'"
      :visible.sync="addDialogVisible"
      width="50%"
      @closed="handleCloseDialog"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="80px"
        label-position="right"
        style="width: 95%"
      >
        <el-form-item label="类型ID" prop="id">
          <el-input
            v-model="addForm.id"
            placeholder="类型ID"
            maxlength="64"
            clearable
            :disabled="edit"
          />
        </el-form-item>
        <el-form-item label="类型名称" prop="name">
          <el-input
            v-model="addForm.name"
            placeholder="类型名称"
            maxlength="64"
            clearable
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit ? updateCategory() : handleAdd()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCategory_list,
  addCategory,
  delCartgory,
  getCartgoryById,
  updateCartgory,
} from "@/api/building";

export default {
  name: "bid_type",
  data() {
    return {
      edit: false,
      listLoading: true,
      categoryList: [],
      addDialogVisible: false,
      addForm: {
        id: "",
        name: "",
      },
      addFormRules: {
        id: [{ required: true, message: "请输入类型ID", trigger: "blur" }],
        name: [{ required: true, message: "请输入类型名称", trigger: "blur" }],
      },
      listQuery: {
        name: "",
        page: 1,
        page_size: 20,
      },
    };
  },
  created() {
    this.getCategory();
  },
  methods: {
    //获取列表
    getCategory() {
      this.listLoading = true;
      getCategory_list(this.listQuery)
        .then((res) => {
          if (res) {
            this.total = res.length;
            this.categoryList = res;
            this.listLoading = false;
          }
        })
        .catch((error) => {
          console.log(error.message);
          this.listLoading = false;
        });
    },
    //点击添加按钮，弹窗显示
    addCategory() {
      this.edit = false;
      this.addDialogVisible = true;
    },
    //确定添加工程事件
    handleAdd() {
      this.$refs.addFormRef.validate((valid) => {
        if (!valid) {
          this.$message.error("请填写必填项");
          this.addForm = {};
          return;
        } else {
          addCategory(this.addForm)
            .then((res) => {
              if (res) {
                this.addDialogVisible = false;
                this.$message.success("添加成功！");
                this.addForm = {};
                this.getCategory();
              }
            })
            .catch((err) => {
              console.log(err);
            });
        }
      });
    },
    //回显信息，弹框显示
    handleEdit(info) {
      this.edit = true;
      getCartgoryById(info).then((res) => {
        this.addForm = res;
      });
      this.addDialogVisible = true;
    },
    //更新信息
    updateCategory() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        else {
          const res = await updateCartgory(this.addForm);
          const { id } = res;
          if (id > 0) {
            this.$message.success("修改成功");
            this.addDialogVisible = false;
            this.getCategory();
          }
        }
      });
    },
    //删除
    async handleDelete(id) {
      const removeResult = await this.$confirm("是否确定删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch((err) => err);
      if (removeResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      delCartgory(id)
        .then((res) => {
          this.$message.success("删除成功！");
          this.getCategory();
        })
        .catch((err) => err);
    },
    //关闭弹窗事件
    handleCloseDialog() {
      this.addForm = {};
      this.addDialogVisible = false;
      this.$refs.addFormRef.resetFields();
    },
  },
};
</script>
<style lang="scss" scoped>
</style>